//
//  ICPageControl.swift
//  ICFoundationFramework
//
//  Created by cuichengcheng on 2017/12/5.
//  Copyright © 2017年 cuichengcheng. All rights reserved.
//

import UIKit
private let defaultDotW:CGFloat = 8
private let dotVsItemRation:CGFloat = 0.6
class ICPageControl: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    private let pageControlCellId = "pageControlCellId"
    var dotW:CGFloat = defaultDotW {
        didSet{
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    var pageControlAlignment:ICPageControlAlignment = .middle {
        didSet{
            if pageControlAlignment == .right {
                self.setNeedsLayout()
                self.layoutIfNeeded()
            }
        }
    }
    var currentPage:Int = 0 {
        didSet{
            carouselCV.selectItem(at: IndexPath(item:currentPage, section:0), animated: true, scrollPosition: .centeredHorizontally)
        }
    }
    var pageW:CGFloat = 0
    let layout = UICollectionViewFlowLayout();
    private var lastSelected = 0
    lazy var carouselCV:UICollectionView = {
        let cv = UICollectionView.init(frame: .zero, collectionViewLayout: self.layout);
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        cv.backgroundColor = .clear
        cv.isPagingEnabled = true
        cv.delegate = self
        cv.dataSource = self
        cv.showsVerticalScrollIndicator = false;
        cv.showsHorizontalScrollIndicator = false;
        cv.register(ICPageControlCell.classForCoder(), forCellWithReuseIdentifier: pageControlCellId)
        self.addSubview(cv)
        return cv;
    }();
    var dotNum:Int = 0 {
        didSet{
            pageW = dotW*CGFloat(dotNum)/dotVsItemRation
            self.carouselCV.reloadData()
        }
    }
    var dotColor:UIColor = UIColor.white {
        didSet{
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    init() {
        super.init(frame: .zero)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if dotNum <= 0 {return}
        carouselCV.bounds = CGRect(x: 0, y: 0, width: pageW, height: 30)
        if pageControlAlignment == .middle {
           carouselCV.center = CGPoint(x: self.center.x, y: frame.size.height/2)
        }else {
            carouselCV.center = CGPoint(x: frame.maxX-carouselCV.bounds.size.width/2, y: frame.size.height/2)
        }
        
        layout.itemSize = CGSize(width: dotW/dotVsItemRation, height: carouselCV.bounds.size.height)
        carouselCV.selectItem(at: IndexPath(item:0, section:0), animated: false, scrollPosition: .centeredHorizontally)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dotNum
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: pageControlCellId, for: indexPath)
        return cell
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

class ICPageControlCell: UICollectionViewCell {
    private let dotV:UIImageView = UIImageView()
    var dotW:CGFloat = defaultDotW {
        didSet{
            dotV.bounds = CGRect(x: 0, y: 0, width: self.dotW, height: self.dotW)
            self.img_y = createImg(color: currentPageIndicatorTintColor, rect: CGRect(x: 0, y: 0, width: dotW, height: dotW))?.kt_addCorner(radius: dotW/2.0, CGSize(width: dotW, height: dotW))
            self.img_n = createImg(color: pageIndicatorTintColor, rect: CGRect(x: 0, y: 0, width: dotW, height: dotW))?.kt_addCorner(radius: dotW/2.0, CGSize(width: dotW, height: dotW))
        }
    }
    private var img_y = createImg(color: .white, rect: CGRect(x: 0, y: 0, width: defaultDotW, height: defaultDotW))?.kt_addCorner(radius: defaultDotW/2.0, CGSize(width: defaultDotW, height: defaultDotW))
    private var img_n = createImg(color: UIColor(red: 0, green: 0, blue: 0, alpha: 0.2), rect: CGRect(x: 0, y: 0, width: defaultDotW, height: defaultDotW))?.kt_addCorner(radius: defaultDotW/2.0, CGSize(width: defaultDotW, height: defaultDotW))
    
    var currentPageIndicatorTintColor:UIColor = .white {
        didSet{
            self.img_y = createImg(color: currentPageIndicatorTintColor, rect: CGRect(x: 0, y: 0, width: defaultDotW, height: defaultDotW))?.kt_addCorner(radius: defaultDotW/2.0, CGSize(width: defaultDotW, height: defaultDotW))
        }
    }
    var pageIndicatorTintColor:UIColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2) {
        didSet{
            self.img_n = createImg(color: pageIndicatorTintColor, rect: CGRect(x: 0, y: 0, width: defaultDotW, height: defaultDotW))?.kt_addCorner(radius: defaultDotW/2.0, CGSize(width: defaultDotW, height: defaultDotW))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        _initContentSubviews()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        dotW = frame.size.width * dotVsItemRation
    }
    
    override func awakeFromNib() {
        _initContentSubviews()
    }
    
    func _initContentSubviews() {
        
        let rect = CGRect(x: 0, y: 0, width: dotW, height: dotW)
        dotV.image = img_n
        self.contentView.addSubview(dotV)
        dotV.center = self.contentView.center
        dotV.bounds = rect
    }
    
    override var isSelected: Bool {
        didSet{
            dotV.image = isSelected ? img_y : img_n
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
